package com.xysq.activity;

import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.rockcent.action.CallbackListener;
import com.xysq.lemon.R;
import com.xysq.util.ToastUtil;
import com.xysq.util.UserInfoKeeper;

import butterknife.InjectView;

public class EvaluateActivity extends BaseActivity implements View.OnClickListener {

    @InjectView(R.id.img_one_star)
    ImageView oneStarImg;
    @InjectView(R.id.img_two_star)
    ImageView twoStarImg;
    @InjectView(R.id.img_three_star)
    ImageView threeStarImg;
    @InjectView(R.id.img_four_star)
    ImageView fourStarImg;
    @InjectView(R.id.img_five_star)
    ImageView fiveStarImg;
    @InjectView(R.id.txt_evaluate_result)
    TextView evaluateResultTxt;
    @InjectView(R.id.btn_confirm_receive)
    Button confirmReceiveBtn;
    @InjectView(R.id.edit_evaluate)
    EditText evaluateEdit;
    @InjectView(R.id.ibtn_back)
    ImageButton backIbtn;
    int selectedStar = 0;
    int verificationId = 0;
    int couponModelId = 0;
    String orderId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_evaluate);
        getExtra();
        initViewAndData();
    }

    private void getExtra() {
        Intent intent = getIntent();
        verificationId = intent.getIntExtra("verificationId", 0);
        couponModelId = intent.getIntExtra("couponModelId", 0);
        orderId = intent.getStringExtra("orderId");
    }

    private void initViewAndData() {
        backIbtn.setOnClickListener(this);
        oneStarImg.setOnClickListener(this);
        twoStarImg.setOnClickListener(this);
        threeStarImg.setOnClickListener(this);
        fourStarImg.setOnClickListener(this);
        fiveStarImg.setOnClickListener(this);
        confirmReceiveBtn.setOnClickListener(this);
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onClick(View view) {
        if (view == backIbtn) {
            finish();
        }
        if (view == oneStarImg) {
            selectedStar = 1;
            oneStarImg.setImageDrawable(getResources().getDrawable(R.drawable.star_selected));
            twoStarImg.setImageDrawable(getResources().getDrawable(R.drawable.star_unselected));
            threeStarImg.setImageDrawable(getResources().getDrawable(R.drawable.star_unselected));
            fourStarImg.setImageDrawable(getResources().getDrawable(R.drawable.star_unselected));
            fiveStarImg.setImageDrawable(getResources().getDrawable(R.drawable.star_unselected));
            evaluateResultTxt.setText("什么鬼！");

        }
        if (view == twoStarImg) {
            selectedStar = 2;
            oneStarImg.setImageDrawable(getResources().getDrawable(R.drawable.star_selected));
            twoStarImg.setImageDrawable(getResources().getDrawable(R.drawable.star_selected));
            threeStarImg.setImageDrawable(getResources().getDrawable(R.drawable.star_unselected));
            fourStarImg.setImageDrawable(getResources().getDrawable(R.drawable.star_unselected));
            fiveStarImg.setImageDrawable(getResources().getDrawable(R.drawable.star_unselected));
            evaluateResultTxt.setText("什么鬼？");
        }
        if (view == threeStarImg) {
            selectedStar = 3;
            oneStarImg.setImageDrawable(getResources().getDrawable(R.drawable.star_selected));
            twoStarImg.setImageDrawable(getResources().getDrawable(R.drawable.star_selected));
            threeStarImg.setImageDrawable(getResources().getDrawable(R.drawable.star_selected));
            fourStarImg.setImageDrawable(getResources().getDrawable(R.drawable.star_unselected));
            fiveStarImg.setImageDrawable(getResources().getDrawable(R.drawable.star_unselected));
            evaluateResultTxt.setText("勉强咯~");
        }
        if (view == fourStarImg) {
            selectedStar = 4;
            oneStarImg.setImageDrawable(getResources().getDrawable(R.drawable.star_selected));
            twoStarImg.setImageDrawable(getResources().getDrawable(R.drawable.star_selected));
            threeStarImg.setImageDrawable(getResources().getDrawable(R.drawable.star_selected));
            fourStarImg.setImageDrawable(getResources().getDrawable(R.drawable.star_selected));
            fiveStarImg.setImageDrawable(getResources().getDrawable(R.drawable.star_unselected));
            evaluateResultTxt.setText("还可以~");
        }
        if (view == fiveStarImg) {
            selectedStar = 5;
            oneStarImg.setImageDrawable(getResources().getDrawable(R.drawable.star_selected));
            twoStarImg.setImageDrawable(getResources().getDrawable(R.drawable.star_selected));
            threeStarImg.setImageDrawable(getResources().getDrawable(R.drawable.star_selected));
            fourStarImg.setImageDrawable(getResources().getDrawable(R.drawable.star_selected));
            fiveStarImg.setImageDrawable(getResources().getDrawable(R.drawable.star_selected));
            evaluateResultTxt.setText("给力！");
        }
        if (view == confirmReceiveBtn) {
            if (selectedStar == 0) {
                ToastUtil.showShort(EvaluateActivity.this, "麻烦动动手选颗星星亲！");
            } else {
                if (evaluateEdit.getText().toString().trim().length() == 0) {
                    ToastUtil.showShort(EvaluateActivity.this, "麻烦动动小手在评价框里吐槽一句亲");
                } else {
                    appAction.addComment(orderId, UserInfoKeeper.readPhoneNumber(EvaluateActivity.this), couponModelId, selectedStar, evaluateEdit.getText().toString(), "1", verificationId, new CallbackListener<Void>() {
                        @Override
                        public void onSuccess(Void data) {
                            ToastUtil.showShort(EvaluateActivity.this, "评价成功");
                            finish();
                        }

                        @Override
                        public void onFailure(String errorCode, String message) {
                            ToastUtil.showShort(EvaluateActivity.this, message);
                        }
                    });
                }
            }
        }
    }
}
